package org.jsbd.boss.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.ILogStatisticsActionDao;
import org.jsbd.boss.dao.IWebLogCreateTableDao;
import org.jsbd.boss.domian.LogStatisticsActionBean;
import org.jsbd.boss.service.ILogStatisticsActionService;
import org.jsbd.boss.util.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("logStatisticsActionService")
public class LogStatisticsActionServiceImpl implements ILogStatisticsActionService {

	@Autowired
	private ILogStatisticsActionDao logStatisticsActionDao;
	@Autowired
	private IWebLogCreateTableDao webLogCreateTableDao;
	@Autowired
	private RedisClient<String,Object> redisClient;
	@Autowired
	private RedisClient<String,Object> redisClientMigu;
	
	@Override
	public void saveLogStatisticsAction(LogStatisticsActionBean logStatisticsAction){
		logStatisticsActionDao.saveLogStatisticsAction(logStatisticsAction);
	}

	@Override
	public void updateLogStatisticsAction(LogStatisticsActionBean logStatisticsAction){
		logStatisticsActionDao.updateLogStatisticsAction(logStatisticsAction);
	}

	@Override
	public LogStatisticsActionBean getLogStatisticsAction(LogStatisticsActionBean logStatisticsAction) {
		return logStatisticsActionDao.getLogStatisticsAction(logStatisticsAction);
	}

	@Override
	public List<LogStatisticsActionBean> getLogStatisticsActionList(LogStatisticsActionBean logStatisticsAction) {
		return logStatisticsActionDao.getLogStatisticsActionList(logStatisticsAction);
	}

	@Override
	public Map<String, Object> getLogStatisticsActionListPage(DataGridModel page, LogStatisticsActionBean logStatisticsAction) {
		return logStatisticsActionDao.getLogStatisticsActionListPage(page, logStatisticsAction);
	}

	@Override
	public Integer getLogStatisticsActionCount(LogStatisticsActionBean logStatisticsAction) {
		return logStatisticsActionDao.getLogStatisticsActionCount(logStatisticsAction);
	}
	
	@Override
	public void deleteLogStatisticsAction(LogStatisticsActionBean logStatisticsAction) {
		redisClientMigu.remove(CacheKey.LOG_ACTION_HASH_KEY+logStatisticsAction.getId());
		logStatisticsActionDao.deleteLogStatisticsAction(logStatisticsAction);
	}

	@Override
	public void updateStatus(LogStatisticsActionBean logStatisticsAction) {
		if(logStatisticsAction.getStatus()==1) {
			createTable(logStatisticsAction);
		}
		logStatisticsActionDao.updateStatus(logStatisticsAction);
		
		redisClient.remove(CacheKey.LOG_LOG_ACTION_KEY);
		redisClient.remove(CacheKey.LOG_LOG_ACTION_TABLE_KEY+logStatisticsAction.getActionTableName());
	}
	
	private void createTable(LogStatisticsActionBean logStatisticsAction) {
		String tableName=logStatisticsAction.getSyncTableName();
	    if(StringUtils.isBlank(tableName)) {
	    	return;
	    }
	    
	    String existTableName=webLogCreateTableDao.getTableByName(tableName);
	    if(StringUtils.isNotBlank(existTableName)) {
	    	return;
	    }
	    
	    String sql=createSqlByActionBean(logStatisticsAction);
	    webLogCreateTableDao.createTable(sql);
	}
	
	
	private String createSqlByActionBean(LogStatisticsActionBean logStatisticsAction) {
		String tableName=logStatisticsAction.getSyncTableName();
		
		StringBuffer unique=new StringBuffer();
		StringBuffer uniqueKey=new StringBuffer();
		
		StringBuffer buffer=new StringBuffer();
		buffer.append("CREATE TABLE ").append("`"+tableName+"`");
		buffer.append(" ( ");
		buffer.append("`id` int(10) NOT NULL AUTO_INCREMENT,");
		buffer.append("`create_time` datetime DEFAULT NULL,");
		buffer.append("`time` varchar(32) DEFAULT NULL,");
		unique.append("`time`");
		uniqueKey.append("idx_").append(tableName).append("_time");
		buffer.append("`count` int(10) DEFAULT NULL,");
		Integer actionType=logStatisticsAction.getActionType();
		if(actionType==LogStatisticsActionBean.SUM) {
			buffer.append("`staistics` double(16,8) DEFAULT NULL,");
		}else if(actionType==LogStatisticsActionBean.AVER) {
			buffer.append("`staistics` double(16,8) DEFAULT NULL,");
			buffer.append("`average` double(16,8) DEFAULT NULL,");
		}
		
		String conditionKey=logStatisticsAction.getConditionKey();
		if(StringUtils.isNotBlank(conditionKey)) {
			String[] conditonArr=conditionKey.split(";");
			for(String conditon:conditonArr) {
				int index=-1;
				while((index=conditon.indexOf("->"))>0) {
					conditon=conditon.substring(index+2);
				}
				
				String[] keyArr=conditon.split(",");
				for(String key:keyArr) {
					int idx=key.indexOf("=");
					int length=128;
					if(idx>0) {
						length=key.substring(idx+1).length()*2;
						key=key.substring(0,idx);
					}
					buffer.append("`"+key+"` varchar("+length+") DEFAULT NULL,");
					unique.append(",`"+key+"`");
					uniqueKey.append("_"+key);
				}
			}
		}
		buffer.append("PRIMARY KEY (`id`),");
		buffer.append("UNIQUE KEY `"+uniqueKey.toString()+"` ("+unique+")");
		buffer.append(" )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
		return buffer.toString();
	}
	
}
